package com.mybed.crm.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mybed.crm.mapper.CluesMapper;
import com.mybed.crm.mapper.UserMapper;
import com.mybed.crm.pojo.dto.CluedDto;
import com.mybed.crm.pojo.dto.CluesDto;
import com.mybed.crm.pojo.dto.GainDto;
import com.mybed.crm.pojo.pojo.*;
import com.mybed.crm.result.PageResult;
import com.mybed.crm.result.Result;
import com.mybed.crm.service.CluesService;
import com.mybed.crm.service.UserService;
import com.mybed.crm.utils.thread.UserThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class CluesServiceImpl extends ServiceImpl<CluesMapper,Clues> implements CluesService {
    private PageResult pageResult = new PageResult();


    @Autowired
    private CluesMapper cluesMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private UserMapper userMapper;





    @Override
    public PageResult cluesListQuery(CluesDto cluesDto) {
        IPage<Clues> page1 = new Page<>(cluesDto.getPageNum(), cluesDto.getPageSize());
        IPage<Clues> page = cluesMapper.selectCluesList(page1,null,cluesDto);
        pageResult.insertData(page);
        return pageResult;
    }

    @Override
    public Result<Clues> lookClue(Long id) {
        CluesDto cluesDto = new CluesDto();
        cluesDto.setId(id);
        IPage<Clues> page1 = new Page<>();
        IPage<Clues> ipage = cluesMapper.selectCluesList(page1, null, cluesDto);
        Clues clues = ipage.getRecords().get(0);
        log.info("拿到的clues对象内容是：{}",clues);
        return Result.success(clues);
    }

    @Override
    public PageResult cluesPoolQuery(CluesDto cluesDto) {
        IPage<Clues> page1 = new Page<>(cluesDto.getPageNum(), cluesDto.getPageSize());
        IPage<Clues> page = cluesMapper.selectPoolsList(page1,null,cluesDto);
        pageResult.insertData(page);
        return pageResult;
    }

    @Override
    public void addClues(Clues clues) {
        String UsinguserName = userService.getInfo().getUser().getUserName();
        String userid = UserThreadLocalUtil.getUser();

        LocalDateTime now = LocalDateTime.now();
        log.info("时间是：{}",now);

        clues.setStatus("1");
        clues.setCreateBy(UsinguserName); // createname
        clues.setUpdateBy(UsinguserName);
        clues.setOwner(UsinguserName);
        clues.setOwnerTime(LocalDateTime.now());
        clues.setCreateTime(LocalDateTime.now());
        clues.setUpdateTime(LocalDateTime.now());
        cluesMapper.insertClues(clues);
    }

    @Override
    public void addCluesPool(CluesPool cluesPool) {
        String UsinguserName = userService.getInfo().getUser().getUserName();
        Clues clues = new Clues();
        clues.setUpdateTime(LocalDateTime.now());
        clues.setUpdateBy(UsinguserName);
        clues.setStatus("4");
        clues.setId(cluesPool.getCluesId());
        cluesMapper.updateClues(clues);
        cluesMapper.setClues(cluesPool);
    }

    @Override
    public void keepClues(CluedDto cluedDto) {
        String UsinguserName = userService.getInfo().getUser().getUserName();
        //修改clues中的内容 用更新方法
        //将状态改为2，跟进中
        Clues clues = new Clues();
        BeanUtils.copyProperties(cluedDto , clues);

        clues.setId(cluedDto.getClueId());
        clues.setUpdateTime(LocalDateTime.now());
        clues.setNextTime(cluedDto.getNextTime());
        clues.setUpdateBy(UsinguserName);
        clues.setStatus("2");
        cluesMapper.updateClues(clues);
        //把要修改的内容 放到记录表中
        CluesRecord cluesRecord = new CluesRecord();
        BeanUtils.copyProperties(cluedDto,cluesRecord);
        cluesRecord.setCluesId(cluedDto.getClueId());
        cluesRecord.setCreateBy(UsinguserName);
        cluesRecord.setCreateTime(LocalDateTime.now());
        cluesRecord.setUpdateBy(UsinguserName);
        cluesRecord.setUpdateTime(LocalDateTime.now());
        cluesMapper.setCluesRecord(cluesRecord);
    }

    @Override
    public PageResult getCluedlist(long clueId) {
        IPage<CluesRecord> page1 = new Page<>();
        IPage<CluesRecord> page = cluesMapper.selectCluesRecordList(page1,null,clueId);
        pageResult.insertData(page);
        return pageResult;
    }

    @Override
    public void putToBusiness(Long clueId) {
        Clues clues = cluesMapper.selectCluesById(clueId);
        log.info("为什么学科是空的呢？：{}",clues);
        Business business = new Business();
        BeanUtils.copyProperties(clues,business);
        business.setSubject(clues.getSubject());
        business.setChannel(Integer.valueOf(clues.getChannel()));
        business.setSex(clues.getSex());
        business.setStatus(1);
        business.setLevel(Integer.valueOf(clues.getLevel()));
        business.setTransfer("1");
        cluesMapper.putToBusiness(business);
        //改为 5 和 1 方便查
        clues.setStatus("5");
        clues.setTransfer("1");
        cluesMapper.updateClues(clues);
    }

    @Override
    public void gainCluesPool(GainDto gainDto) {
        String UsinguserName = userService.getInfo().getUser().getUserName();
        List<Long> ids = gainDto.getIds();
        for (Object id : ids) {
            Clues clues = new Clues();
            clues.setId((Long) id);
            clues.setStatus("1");
            clues.setUpdateBy(UsinguserName);
            clues.setUpdateTime(LocalDateTime.now());
            cluesMapper.updateClues(clues);
        }
    }

    @Override
    public void cluesAssignment(GainDto gainDto) {
        Clues clues = new Clues();
        for (Long id : gainDto.getIds()) {
            log.info("传进来的要分配给谁的本人id为：{}", gainDto.getUserId());
            User user = userMapper.selectById(gainDto.getUserId());

            String UsinguserName = userService.getInfo().getUser().getUserName();
            clues.setId(id);
            clues.setOwner(user.getUserName());
            clues.setAssignBy(UsinguserName);
            clues.setAssignTime(LocalDateTime.now());
            clues.setOwnerTime(LocalDateTime.now());
            clues.setUpdateBy(UsinguserName);
            clues.setUpdateTime(LocalDateTime.now());
            clues.setUserId(gainDto.getUserId());
            cluesMapper.updateClues(clues);
        }

    }


}
